package com.example.demo.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.StringWriter;
import java.util.Map;

@Slf4j
public class FreemarkerUtil {
    private static final String path = FreemarkerUtil.class.getClassLoader().getResource("").getPath();

    public static String generateString(String templateFileName, String templateDirectory, Map<String, Object> datas) {
        try{
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);

            // 设置默认编码
            configuration.setDefaultEncoding("UTF-8");

            // 设置模板所在文件夹
            configuration.setDirectoryForTemplateLoading(new File(path + templateDirectory));

            // 生成模板对象
            Template template = configuration.getTemplate(templateFileName);

            // 将datas写入模板并返回
            try (StringWriter stringWriter = new StringWriter()) {
                template.process(datas, stringWriter);
                stringWriter.flush();
                return stringWriter.getBuffer().toString();
            }

        }catch (Exception e){
            log.error("",e);
        }

        return null;
    }
}
